const mongoose = require('mongoose');
const { dbUrl } = require('../config')
const options = {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify:false,
    poolSize: 10
}
// 连接mongodb数据库
mongoose.set('useCreateIndex', true)
mongoose.connect(dbUrl, options);

const db = mongoose.connection;

// 连接失败时
db.on('error', err => {
    console.log('Mongo DB 数据库连接失败', err);
});
// 连接成功时
db.once('open', function () {
    console.log('Mongo DB 数据库连接成功');
});

// 由模式组织导出模型类（模型构造函数）
module.exports = {
    User: mongoose.model('User', require('./modules/user'), 'user'),
    App: mongoose.model('App', require('./modules/app'), 'app'),
    User_BackUp: mongoose.model('User_BackUp', require('./modules/user_backup'), 'user_backup')
}
